//
//  UIViewController+itv.m
//  itv
//
//  Created by Zhang Yinghui on 11-12-29.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "BKViewController.h"
#import "BKKit.h"
#import "UINavigationBar+x.h"
#import "Theme.h"
#import "BKIndicator.h"
#import "BKImageView.h"
#import "UILabel+x.h"
#import <AFNetworking/UIImageView+AFNetworking.h>
@interface BKViewController ()
@property (nonatomic, retain) UIView *indicatorView;
@property (nonatomic, retain) NSMutableDictionary *requestPool;
@property (nonatomic, retain) NSString *navTitle;
@end


@implementation BKViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.requestPool = [NSMutableDictionary dictionary];
    }
    return self;
}
- (id)init{
    if (self = [super init]) {
        self.requestPool = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)setNavigationBarBackgroundImage:(UIImage *)backgroundImage{
    UINavigationBar *navBar = [self.navigationController navigationBar];
    if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarPosition:barMetrics:)]) {
        
        backgroundImage = [backgroundImage resizableImageWithCapInsets:UIEdgeInsetsMake(backgroundImage.size.height/2, backgroundImage.size.width/2, backgroundImage.size.height/2, backgroundImage.size.width/2)];
        [navBar setBackgroundImage:backgroundImage forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault];
    }
    else if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
        UIImage *navigationBarBackground = backgroundImage;
        if (navigationBarBackground)
            [navBar setBackgroundImage:navigationBarBackground forBarMetrics:UIBarMetricsDefault];
    }
}
- (void)loadView{
    [super loadView];
    [self awake];
}
- (void) awake{
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
	_frame = self.view.bounds;
    if (self.navigationController) {
       // _frame.size.height -= self.navigationController.navigationBar.bounds.size.height;
    }
	if (self.tabBarController && !self.hidesBottomBarWhenPushed) {
		//_frame.size.height -= self.tabBarController.tabBar.frame.size.height;
	}
}
- (void)didReceiveMemoryWarning{
    DLOG(@"didReceiveMemoryWarning & reset");
    [super didReceiveMemoryWarning];
    [self reset];
}
- (void)setDropMenuTitle:(NSString *)title{
    if (!self.navigationItem.titleView || ![self.navigationItem.titleView isKindOfClass:[UIButton class]]) {
        UIView *navBar = self.navigationController.navigationBar;
        BKDropTitleView *titleView = [[BKDropTitleView alloc] initWithFrame:CGRectMake(0, 0, navBar.bounds.size.width/2, navBar.bounds.size.height)];
        
        [titleView.titleLabel setFont:THEME_NAV_BAR_TITLE_FONT];
        [titleView.titleLabel setTextColor:THEME_NAV_BAR_TITLE_COLOR];
        [titleView addTarget:self action:@selector(showDropMenu:) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.titleView = titleView;
    }
    [self setTitle:title];
}
- (void)setTitleLabel:(UILabel *)titleLabel{
    ////
    _titleLabel = titleLabel;
    self.navigationItem.titleView = titleLabel;
    titleLabel.text = self.navTitle;
}
- (void)setTitle:(NSString *)title withImageURL:(NSURL *)imageURL{
    float iconWidth = 32, gap = 5;
    float w = iconWidth, x = 0;
    if (title) {
        CGRect titleBounds = [title boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:THEME_NAV_BAR_TITLE_FONT} context:nil];
        w += titleBounds.size.width+gap;
    }
    w = MIN(w, self.navigationController.navigationBar.bounds.size.width*0.66);
    
    
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w, 40)];
    
    CGRect imgFrame = CGRectMake(0, titleView.bounds.size.height/2-iconWidth/2, iconWidth, iconWidth);
    BKImageView *thumbImageView = [[BKImageView alloc] initWithFrame:imgFrame];
    thumbImageView.contentMode = UIViewContentModeScaleAspectFit;
    [thumbImageView setImageWithURL:imageURL];
    
    [titleView addSubview:thumbImageView];
    thumbImageView = nil;
    
    x += imgFrame.size.width + gap;
    
    UILabel *titleLabel = createLabel(CGRectMake(x, 0, w-x, titleView.bounds.size.height), THEME_NAV_BAR_TITLE_FONT, nil, THEME_NAV_BAR_TITLE_COLOR, THEME_NAV_BAR_TITLE_SHADOW_COLOR, CGSizeZero, NSTextAlignmentLeft, 1, NSLineBreakByTruncatingTail);
    titleLabel.text = title;
    
    [titleLabel.layer setShadowOpacity:THEME_NAV_BAR_TITLE_SHADOW_COLOR?1:0];
    [titleLabel.layer setShadowColor:[THEME_NAV_BAR_TITLE_SHADOW_COLOR CGColor]];
    [titleLabel.layer setShadowRadius:1];
    [titleLabel.layer setShadowOffset:CGSizeMake(0, 1)];
    [titleView addSubview:titleLabel];
    
    self.navigationItem.titleView = titleView;
    titleView = nil;
}
- (void)setTitle:(NSString *)title{
    [self setNavTitle:title];
    if (self.navigationItem.titleView && [self.navigationItem.titleView isKindOfClass:[UIButton class]]) {
        [(UIButton *)self.navigationItem.titleView setTitle:title forState:UIControlStateNormal];
    }else if(self.navigationItem.titleView && [self.navigationItem.titleView isKindOfClass:[UILabel class]]){
        [(UILabel *)self.navigationItem.titleView setText:title];
    }
}
- (void)viewDidLoad{
    [super viewDidLoad];
    [self awake];
    [self.view setBackgroundColor:THEME_VIEW_BG_COLOR];
    if (self.navigationItem && !self.titleLabel) {
        CGRect rect = CGRectInset(self.navigationController.navigationBar.bounds, 60, 0);
        UILabel *titleLabel = createLabel(rect, THEME_NAV_BAR_TITLE_FONT, nil, THEME_NAV_BAR_TITLE_COLOR, nil, CGSizeZero, NSTextAlignmentCenter, 0, NSLineBreakByTruncatingTail);
        [titleLabel setText:self.navTitle];
        DLOG(@"%@",THEME_NAV_BAR_TITLE_SHADOW_COLOR);
        [titleLabel.layer setShadowOpacity:THEME_NAV_BAR_TITLE_SHADOW_COLOR?1:0];
        [titleLabel.layer setShadowColor:[THEME_NAV_BAR_TITLE_SHADOW_COLOR CGColor]];
        [titleLabel.layer setShadowRadius:1];
        [titleLabel.layer setShadowOffset:CGSizeMake(0, 1)];
        [self.navigationItem setTitleView:titleLabel];
        [self setTitleLabel:titleLabel];
    }
}
- (void)addBackButton{
    if (!self.navigationItem) {
        return;
    }
    UIImage *backImg = [UIImage imageNamed:@"back"];
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, backImg.size.width, backImg.size.height)];
    [btn addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:backImg forState:UIControlStateNormal];
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:btn];
    [self.navigationItem setLeftBarButtonItem:backButton animated:YES];
}
- (void)goBack:(id)sender{
    [self dismissViewControllerAnimated:YES
                             completion:nil];
}
//- (void)viewDidUnload{
//    [super viewDidUnload];
//    DLOG(@"viewDidUnload & reset");
//    [self reset];
//}
- (void)reset{
    DLOG(@"[%@] reset...", NSStringFromClass([self class]));
}

- (BOOL)shouldPopViewController:(UIViewController *)controller{
    return YES;
}
- (void)popViewControllerAnimated:(BOOL)animated{
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)popViewController:(id)sender {
    if ( ![self shouldPopViewController:self]) {
        return;
    }
    [self popViewControllerAnimated:YES];
}

/********/
- (void)showMessage:(NSString *)msg duration:(float)duration{
    [BKIndicator showMessage:msg duration:duration inView:self.view  completion:nil];
}
- (void)showMessage:(NSString *)msg{
    [BKIndicator showMessage:msg inView:self.view];
}
- (void)showMessageAndFadeOut:(NSString *)msg{
    [BKIndicator showMessage:msg duration:2.0 inView:self.view completion:nil];
}
- (void)fadeOutAfterDelay:(float)t{
    [BKIndicator fadeOutWithDelay:t completion:nil];
}
- (void)fadeOut{
    [self fadeOutAfterDelay:0];
}

- (void)setHttpRequest:(id)request forKey:(NSString *)key{
    //AFHTTPRequestOperation *req = [self.requestPool valueForKey:key];
    //if (req && ![req isCancelled]) {
    //    [req cancel];
    //}
    [self.requestPool setValue:request forKey:key];    
}
- (void)showDropMenu:(id)sender{}
- (void)dealloc{
//    for (NSString *key in [_requestPool allKeys]) {
        //AFHTTPRequestOperation *req = [self.requestPool valueForKey:key];
        //[req cancel];
//    }
    //[super dealloc];
}
- (BOOL)shouldAutorotate{
    return NO;
}
@end
